package com.ruoyi.produce.process.costAccounting.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.produce.process.costAccounting.domain.CostAccounting;
import com.ruoyi.produce.process.costAccounting.domain.CostAccountingCond;
import com.ruoyi.produce.process.costAccounting.service.ICostAccountingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/produce/cost/accounting")
@Api(value = "成本结算信息模块", tags = "成本结算信息接口")
public class CostAccountingController extends BaseController {
    @Autowired
    private ICostAccountingService costAccountingService;

    /**
     * 获取成本结算
     * @param cond
     * @return
     */
    @ApiOperation(value = "获取成本结算列表",notes = "查询所有成本结算信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody CostAccountingCond cond){
        startPage();
        List<CostAccounting> costAccountingList = costAccountingService.selectCostAccountingList(cond);
        return getDataTable(costAccountingList);
    }

    /**
     * 导出自成本结算
     * @param cond
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody CostAccountingCond cond)
    {
        List<CostAccounting> costAccountingList = costAccountingService.selectCostAccountingList(cond);
        ExcelUtil<CostAccounting> util = new ExcelUtil<>(CostAccounting.class);
        return util.exportExcel(costAccountingList, "成本结算数据");
    }

    /**
     * 新增自成本结算
     * @param costAccounting
     * @return
     */
    @ApiOperation(value = "新增自成本结算", notes = "新增自成本结算")
    @ApiImplicitParam(name = "costAccounting", value = "新增自成本结算", dataType = "CostAccounting")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CostAccounting costAccounting){
        if (costAccountingService.selectCostAccountingByPlanSelfId(costAccounting.getPlanSelfId())) {
            return AjaxResult.error("该生产计划已经结算过，无法重复添加");
        }
        return toAjax(costAccountingService.insertCostAccounting(costAccounting));
    }

    /**
     * 修改成本结算
     * @param costAccounting
     * @return
     */
    @ApiOperation(value = "修改成本结算", notes = "修改成本结算")
    @ApiImplicitParam(name = "costAccounting", value = "修改成本结算", dataType = "CostAccounting")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody CostAccounting costAccounting)
    {
        return toAjax(costAccountingService.updateCostAccounting(costAccounting));
    }

    /**
     * 删除成本结算
     * 仅修改删除标志位
     * @param costAccounting
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody CostAccounting costAccounting)
    {

        return toAjax(costAccountingService.updateCostAccountingDeleteFlag(costAccounting));
    }
}
